home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH3 / SRC / PALDRAW.FRM < prev    next >
Text File  |  1996-05-01  |  14KB  |  510 lines

  1. VERSION 4.00
  2. Begin VB.Form PalDrawForm 
  3.    Caption         =   "PalDraw"
  4.    ClientHeight    =   4260
  5.    ClientLeft      =   1455
  6.    ClientTop       =   1440
  7.    ClientWidth     =   7200
  8.    DrawMode        =   14  'Copy Pen
  9.    Height          =   4950
  10.    Left            =   1395
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   4260
  13.    ScaleWidth      =   7200
  14.    Top             =   810
  15.    Width           =   7320
  16.    Begin VB.PictureBox ForeColorSwatch 
  17.       AutoRedraw      =   -1  'True
  18.       Height          =   500
  19.       Left            =   840
  20.       ScaleHeight     =   435
  21.       ScaleWidth      =   435
  22.       TabIndex        =   12
  23.       Top             =   1440
  24.       Width           =   500
  25.    End
  26.    Begin VB.PictureBox FillColorSwatch 
  27.       AutoRedraw      =   -1  'True
  28.       Height          =   500
  29.       Left            =   840
  30.       ScaleHeight     =   435
  31.       ScaleWidth      =   435
  32.       TabIndex        =   9
  33.       Top             =   2040
  34.       Width           =   500
  35.    End
  36.    Begin VB.ComboBox FillCombo 
  37.       Height          =   315
  38.       ItemData        =   "PALDRAW.frx":0000
  39.       Left            =   840
  40.       List            =   "PALDRAW.frx":001C
  41.       Style           =   2  'Dropdown List
  42.       TabIndex        =   8
  43.       Top             =   1080
  44.       Width           =   1815
  45.    End
  46.    Begin VB.ComboBox DrawCombo 
  47.       Height          =   315
  48.       ItemData        =   "PALDRAW.frx":008F
  49.       Left            =   840
  50.       List            =   "PALDRAW.frx":00A8
  51.       Style           =   2  'Dropdown List
  52.       TabIndex        =   6
  53.       Top             =   720
  54.       Width           =   1815
  55.    End
  56.    Begin VB.ComboBox ObjectCombo 
  57.       Height          =   315
  58.       ItemData        =   "PALDRAW.frx":00E7
  59.       Left            =   840
  60.       List            =   "PALDRAW.frx":00F7
  61.       Style           =   2  'Dropdown List
  62.       TabIndex        =   3
  63.       Top             =   0
  64.       Width           =   1815
  65.    End
  66.    Begin VB.TextBox WidthText 
  67.       Height          =   285
  68.       Left            =   840
  69.       MaxLength       =   1
  70.       TabIndex        =   2
  71.       Text            =   "1"
  72.       Top             =   360
  73.       Width           =   375
  74.    End
  75.    Begin VB.PictureBox Canvas 
  76.       AutoRedraw      =   -1  'True
  77.       Height          =   4238
  78.       Left            =   2700
  79.       ScaleHeight     =   4185
  80.       ScaleWidth      =   4440
  81.       TabIndex        =   0
  82.       Top             =   0
  83.       Width           =   4500
  84.    End
  85.    Begin VB.Label Label1 
  86.       Caption         =   "FillColor"
  87.       Height          =   255
  88.       Index           =   5
  89.       Left            =   0
  90.       TabIndex        =   11
  91.       Top             =   2160
  92.       Width           =   855
  93.    End
  94.    Begin VB.Label Label1 
  95.       Caption         =   "ForeColor"
  96.       Height          =   255
  97.       Index           =   4
  98.       Left            =   0
  99.       TabIndex        =   10
  100.       Top             =   1560
  101.       Width           =   855
  102.    End
  103.    Begin MSComDlg.CommonDialog FileDialog 
  104.       Left            =   1560
  105.       Top             =   1560
  106.       _version        =   65536
  107.       _extentx        =   847
  108.       _extenty        =   847
  109.       _stockprops     =   0
  110.       cancelerror     =   -1  'True
  111.    End
  112.    Begin VB.Label Label1 
  113.       Caption         =   "FillStyle"
  114.       Height          =   255
  115.       Index           =   3
  116.       Left            =   0
  117.       TabIndex        =   7
  118.       Top             =   1080
  119.       Width           =   855
  120.    End
  121.    Begin VB.Label Label1 
  122.       Caption         =   "DrawStyle"
  123.       Height          =   255
  124.       Index           =   2
  125.       Left            =   0
  126.       TabIndex        =   5
  127.       Top             =   720
  128.       Width           =   855
  129.    End
  130.    Begin VB.Label Label1 
  131.       Caption         =   "DrawWidth"
  132.       Height          =   255
  133.       Index           =   1
  134.       Left            =   0
  135.       TabIndex        =   4
  136.       Top             =   360
  137.       Width           =   855
  138.    End
  139.    Begin VB.Label Label1 
  140.       Caption         =   "Object"
  141.       Height          =   255
  142.       Index           =   0
  143.       Left            =   0
  144.       TabIndex        =   1
  145.       Top             =   0
  146.       Width           =   855
  147.    End
  148.    Begin VB.Menu mnuFile 
  149.       Caption         =   "&File"
  150.       Begin VB.Menu mnuFileLoad 
  151.          Caption         =   "&Load..."
  152.          Shortcut        =   ^L
  153.       End
  154.       Begin VB.Menu mnuFileSep 
  155.          Caption         =   "-"
  156.       End
  157.       Begin VB.Menu mnuFileExit 
  158.          Caption         =   "E&xit"
  159.       End
  160.    End
  161. End
  162. Attribute VB_Name = "PalDrawForm"
  163. Attribute VB_Creatable = False
  164. Attribute VB_Exposed = False
  165. Option Explicit
  166.  
  167. Const OBJ_LINE = 0
  168. Const OBJ_BOX = 1
  169. Const OBJ_CIRCLE = 2
  170. Const OBJ_POINT = 3
  171.  
  172. Dim Obj As Integer  ' The kind of object to draw.
  173.  
  174. Dim Rubberbanding As Boolean
  175. Dim oldmode As Integer
  176. Dim OldStyle As Integer
  177. Dim FirstX As Single
  178. Dim FirstY As Single
  179. Dim LastX As Single
  180. Dim LastY As Single
  181.  
  182. Dim SWid As Single
  183. Dim SHgt As Single
  184. ' ***********************************************
  185. ' Draw the final (non-rubberband) object.
  186. ' ***********************************************
  187. Sub DrawObject()
  188.     ' Draw the object.
  189.     Select Case Obj
  190.         Case OBJ_LINE
  191.             Canvas.Line (FirstX, FirstY)-(LastX, LastY)
  192.         
  193.         Case OBJ_BOX
  194.             Canvas.Line (FirstX, FirstY)-(LastX, LastY), , B
  195.         
  196.         Case OBJ_CIRCLE
  197.             Dim xmid As Single
  198.             Dim ymid As Single
  199.             Dim dx As Single
  200.             Dim dy As Single
  201.             Dim radius As Single
  202.             
  203.             xmid = (FirstX + LastX) / 2
  204.             ymid = (FirstY + LastY) / 2
  205.             dx = Abs(FirstX - LastX)
  206.             dy = Abs(FirstY - LastY)
  207.             If dx < dy Then
  208.                 radius = dx / 2
  209.             Else
  210.                 radius = dy / 2
  211.             End If
  212.             Canvas.Circle (xmid, ymid), radius
  213.     
  214.         Case OBJ_POINT
  215.             Canvas.PSet (LastX, LastY)
  216.             
  217.     End Select
  218. End Sub
  219.  
  220. ' ***********************************************
  221. ' Draw the appropriate kind of rubberband object.
  222. ' ***********************************************
  223. Sub DrawRubberObject()
  224.     Select Case Obj
  225.         Case OBJ_LINE
  226.             Canvas.Line (FirstX, FirstY)-(LastX, LastY)
  227.         
  228.         Case OBJ_BOX
  229.             Canvas.Line (FirstX, FirstY)-(LastX, LastY), , B
  230.         
  231.         Case OBJ_CIRCLE
  232.             Dim xmid As Single
  233.             Dim ymid As Single
  234.             Dim dx As Single
  235.             Dim dy As Single
  236.             Dim radius As Single
  237.             
  238.             xmid = (FirstX + LastX) / 2
  239.             ymid = (FirstY + LastY) / 2
  240.             dx = Abs(FirstX - LastX)
  241.             dy = Abs(FirstY - LastY)
  242.             If dx < dy Then
  243.                 radius = dx / 2
  244.             Else
  245.                 radius = dy / 2
  246.             End If
  247.             Canvas.Circle (xmid, ymid), radius
  248.     
  249.         Case OBJ_POINT
  250.             Canvas.PSet (LastX, LastY)
  251.     
  252.     End Select
  253. End Sub
  254.  
  255.  
  256.  
  257.  
  258.  
  259. ' ***********************************************
  260. ' Start a rubberbanding of some sort.
  261. ' ***********************************************
  262. Private Sub Canvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  263.     ' Let MouseMove know we are rubberbanding.
  264.     Rubberbanding = True
  265.     
  266.     ' Save values so we can restore them later.
  267.     oldmode = Canvas.DrawMode
  268.     OldStyle = Canvas.DrawStyle
  269.     Canvas.DrawMode = vbInvert
  270.     If Obj = OBJ_LINE Then
  271.         Canvas.DrawStyle = vbSolid
  272.     Else
  273.         Canvas.DrawStyle = vbDot
  274.     End If
  275.  
  276.     ' Save the starting coordinates.
  277.     FirstX = X
  278.     FirstY = Y
  279.     
  280.     ' Save the ending coordinates.
  281.     LastX = X
  282.     LastY = Y
  283.     
  284.     ' Draw the appropriate rubberband object.
  285.     DrawRubberObject
  286. End Sub
  287.  
  288.  
  289. ' ***********************************************
  290. ' Continue rubberbanding.
  291. ' ***********************************************
  292. Private Sub Canvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  293.     ' If we are not rubberbanding, do nothing.
  294.     If Not Rubberbanding Then Exit Sub
  295.     
  296.     ' Erase the previous rubberband object.
  297.     DrawRubberObject
  298.  
  299.     ' Save the new ending coordinates.
  300.     LastX = X
  301.     LastY = Y
  302.     
  303.     ' Draw the new rubberband object.
  304.     DrawRubberObject
  305. End Sub
  306.  
  307. ' ***********************************************
  308. ' Finish rubberbanding and draw the object.
  309. ' ***********************************************
  310. Private Sub Canvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  311.     ' If we are not rubberbanding, do nothing.
  312.     If Not Rubberbanding Then Exit Sub
  313.     
  314.     ' We are no longer rubberbanding.
  315.     Rubberbanding = False
  316.     
  317.     ' Erase the previous rubberband object.
  318.     DrawRubberObject
  319.     
  320.     ' Restore the original DrawMode and DrawStyle.
  321.     Canvas.DrawMode = oldmode
  322.     Canvas.DrawStyle = OldStyle
  323.  
  324.     ' Draw the final object.
  325.     DrawObject
  326. End Sub
  327.  
  328.  
  329.  
  330. Private Sub DrawCombo_Click()
  331.     Canvas.DrawStyle = DrawCombo.ListIndex
  332. End Sub
  333.  
  334. Private Sub FillCombo_Click()
  335.     Canvas.FillStyle = FillCombo.ListIndex
  336. End Sub
  337.  
  338.  
  339. ' ***********************************************
  340. ' Allow the user to select a new foreground
  341. ' color.
  342. ' ***********************************************
  343. Private Sub ForeColorSwatch_Click()
  344. Dim popup As New PalettePopup
  345. Dim clr As Long
  346.  
  347.     ' Load the picture to get its palette.
  348.     popup.Picture = Canvas.Picture
  349.     
  350.     ' Fill the popup with palette colors.
  351.     popup.Fill
  352.         
  353.     ' Select the current foreground color.
  354.     popup.SelectedColor = Canvas.ForeColor
  355.     
  356.     ' Let the user select a color.
  357.     popup.Show vbModal
  358.     
  359.     ' Set the selected color using the palete
  360.     ' relative RGB value.
  361.     clr = popup.SelectedColor + &H2000000
  362.     Canvas.ForeColor = clr
  363.     ForeColorSwatch.Line _
  364.         (0, 0)-(SWid, SHgt), clr, BF
  365.     
  366.     Unload popup
  367. End Sub
  368. ' ***********************************************
  369. ' Allow the user to select a new fill color.
  370. ' ***********************************************
  371. Private Sub FillColorSwatch_Click()
  372. Dim popup As New PalettePopup
  373. Dim clr As Long
  374.  
  375.     ' Load the picture to get its palette.
  376.     popup.Picture = Canvas.Picture
  377.     
  378.     ' Fill the popup with palette colors.
  379.     popup.Fill
  380.         
  381.     ' Select the current background color.
  382.     popup.SelectedColor = Canvas.FillColor
  383.     
  384.     ' Let the user select a color.
  385.     popup.Show vbModal
  386.     
  387.     ' Set the selected color using the palete
  388.     ' relative RGB value.
  389.     clr = popup.SelectedColor + &H2000000
  390.     Canvas.FillColor = clr
  391.     FillColorSwatch.Line _
  392.         (0, 0)-(SWid, SHgt), clr, BF
  393.     
  394.     Unload popup
  395. End Sub
  396.  
  397.  
  398. Private Sub Form_Load()
  399.     ' Select the default options.
  400.     DrawCombo.ListIndex = Canvas.DrawStyle
  401.     FillCombo.ListIndex = Canvas.FillStyle
  402.     ObjectCombo.ListIndex = Canvas.FillStyle
  403.     WidthText.Text = Format$(Canvas.DrawWidth)
  404.  
  405.     SWid = ForeColorSwatch.ScaleWidth - 1
  406.     SHgt = ForeColorSwatch.ScaleHeight - 1
  407.         
  408.     ' Fill the color swatches.
  409.     ResetSwatches
  410. End Sub
  411. ' ***********************************************
  412. ' Set the colors in the swatches.
  413. ' ***********************************************
  414. Sub ResetSwatches()
  415. Dim clr As Long
  416.  
  417.     Canvas.Refresh
  418.  
  419.     ' Make the swatches use the same logical
  420.     ' palette as the canvas.
  421.     ForeColorSwatch.Picture = Canvas.Picture
  422.     FillColorSwatch.Picture = Canvas.Picture
  423.  
  424.     ' Start with black again.
  425.     Canvas.ForeColor = vbBlack
  426.     Canvas.FillColor = vbBlack
  427.     ForeColorSwatch.Line (0, 0)-(SWid, SHgt), vbBlack, BF
  428.     FillColorSwatch.Line (0, 0)-(SWid, SHgt), vbBlack, BF
  429. End Sub
  430.  
  431. Private Sub Form_Resize()
  432. Dim wid As Single
  433.  
  434.     wid = ScaleWidth - ObjectCombo.Left - ObjectCombo.Width - 30
  435.     If wid < 100 Then wid = 100
  436.  
  437.     Canvas.Move ScaleWidth - wid, 0, wid, ScaleHeight
  438. End Sub
  439.  
  440. Private Sub mnuFileExit_Click()
  441.     Unload Me
  442. End Sub
  443.  
  444.  
  445. Private Sub mnuFileLoad_Click()
  446. Dim fname As String
  447.  
  448.     ' Allow the user to pick a file.
  449.     On Error Resume Next
  450.     FileDialog.filename = "*.BMP;*.ICO;*.RLE;*.WMF;*.DIB"
  451.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  452.     FileDialog.ShowOpen
  453.     If Err.Number = cdlCancel Then
  454.         Exit Sub
  455.     ElseIf Err.Number <> 0 Then
  456.         Beep
  457.         MsgBox "Error selecting file.", , vbExclamation
  458.         Exit Sub
  459.     End If
  460.     On Error GoTo LoadError
  461.     
  462.     fname = Trim$(FileDialog.filename)
  463.     FileDialog.InitDir = Left$(fname, Len(fname) _
  464.         - Len(FileDialog.FileTitle) - 1)
  465.     Caption = "PalDraw [" & fname & "]"
  466.     
  467.     ' Load the picture.
  468.     Canvas.Picture = LoadPicture(fname)
  469.     ResetSwatches
  470.     Exit Sub
  471.     
  472. LoadError:
  473.     Beep
  474.     MsgBox "Error loading picture " & fname & _
  475.         "." & vbCrLf & Error$, vbExclamation
  476. End Sub
  477.  
  478. Private Sub ObjectCombo_Click()
  479.     Obj = ObjectCombo.ListIndex
  480. End Sub
  481.  
  482.  
  483. ' ***********************************************
  484. ' Change set DrawWidth.
  485. ' ***********************************************
  486. Private Sub WidthText_Change()
  487. Dim wid As Integer
  488.  
  489.     If Not IsNumeric(WidthText.Text) Then Exit Sub
  490.     
  491.     wid = CInt(WidthText.Text)
  492.     If wid < 1 Then Exit Sub
  493.     
  494.     Canvas.DrawWidth = wid
  495. End Sub
  496.  
  497. ' ***********************************************
  498. ' Only allow 1 through 9.
  499. ' ***********************************************
  500. Private Sub WidthText_KeyPress(KeyAscii As Integer)
  501.     If KeyAscii < Asc(" ") Or _
  502.        KeyAscii > Asc("~") Then Exit Sub
  503.     If KeyAscii >= Asc("1") And _
  504.        KeyAscii <= Asc("9") Then Exit Sub
  505.     Beep
  506.     KeyAscii = 0
  507. End Sub
  508.  
  509.  
  510.